package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.util.DateUtils;
import com.epoch.app.crb2btradecenter.model.dto.PageQueryOrderLineESRequest;
import com.epoch.app.mnordercenter.model.dto.OrderLineRepoQueryReq;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @date 2023/5/25 20:59
 */
public class OrderLinePageQueryRequestToPageQueryLineESRequestConvertor {

    public static PageQueryOrderLineESRequest convert(OrderLineRepoQueryReq orderLineRepoQueryReq){
        PageQueryOrderLineESRequest queryOrderLineRequest = new PageQueryOrderLineESRequest();
        queryOrderLineRequest.setId(orderLineRepoQueryReq.getId());
        queryOrderLineRequest.setMainOrderId(orderLineRepoQueryReq.getOrderId());
        queryOrderLineRequest.setOrderBizId(orderLineRepoQueryReq.getOrderBizId());
        queryOrderLineRequest.setOuterOrderId(orderLineRepoQueryReq.getOuterOrderId());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getSubStatusList())){
            queryOrderLineRequest.setSubStatusList(orderLineRepoQueryReq.getSubStatusList());
        }
        queryOrderLineRequest.setFrontOrderBizId(orderLineRepoQueryReq.getFrontOrderBizId());
        queryOrderLineRequest.setCustomerCode(orderLineRepoQueryReq.getCustomerCode());
        queryOrderLineRequest.setReceiveAddressCode(orderLineRepoQueryReq.getReceiveAddressCode());
        //queryOrderLineRequest.setScItemId(orderLineRepoQueryReq.getScItemId());
        queryOrderLineRequest.setScItemCodeLike(orderLineRepoQueryReq.getScItemCodeLike());
        queryOrderLineRequest.setScItemTitleLike(orderLineRepoQueryReq.getScItemTitleLike());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getProductGroupCodeList())){
            queryOrderLineRequest.setProductGroupCodes(orderLineRepoQueryReq.getProductGroupCodeList());
        }
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getOrderTypeList())){
            queryOrderLineRequest.setOrderTypes(orderLineRepoQueryReq.getOrderTypeList());
        }
        queryOrderLineRequest.setChannelCodes(orderLineRepoQueryReq.getBizChannelList());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getSalesOrganizations())){
            queryOrderLineRequest.setSalesOrganizationCodes(orderLineRepoQueryReq.getSalesOrganizations());
        }
        queryOrderLineRequest.setSalesDepartments(orderLineRepoQueryReq.getSalesDepartments());
        queryOrderLineRequest.setSaleChannels(orderLineRepoQueryReq.getSaleChannels());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getCreatorIdList())){
            queryOrderLineRequest.setCreatorIdList(orderLineRepoQueryReq.getCreatorIdList());
        }
        queryOrderLineRequest.setCreatorId(orderLineRepoQueryReq.getCreatorId());
        queryOrderLineRequest.setCreator(orderLineRepoQueryReq.getCreator());
        queryOrderLineRequest.setUpdator(orderLineRepoQueryReq.getUpdator());
        queryOrderLineRequest.setCustomerReferenceNo(orderLineRepoQueryReq.getCustomerReferenceNo());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getCreateReasonList())){
            queryOrderLineRequest.setCreateReason(orderLineRepoQueryReq.getCreateReasonList());
        }
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getRejectReasonList())){
            queryOrderLineRequest.setRejectReason(orderLineRepoQueryReq.getRejectReasonList());
        }
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getShippingWarehouseList())){
            queryOrderLineRequest.setShippingWarehouses(orderLineRepoQueryReq.getShippingWarehouseList());
        }
        queryOrderLineRequest.setDeliveryMethod(orderLineRepoQueryReq.getDeliveryMethod());
        queryOrderLineRequest.setBisUnitOutCodes(orderLineRepoQueryReq.getBisUnitOutCodes());
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getSalesOrganizationOutCodes())){
            queryOrderLineRequest.setSalesOrganizationOutCodes(orderLineRepoQueryReq.getSalesOrganizationOutCodes());
        }
        queryOrderLineRequest.setSalesChannelOutCodes(orderLineRepoQueryReq.getSalesChannelOutCodes());
        queryOrderLineRequest.setSalesDepartmentOutCodes(orderLineRepoQueryReq.getSalesDepartmentOutCodes());
        if(StringUtils.isNotBlank(orderLineRepoQueryReq.getBusinessType())){
            queryOrderLineRequest.setBusinessType(Integer.valueOf(orderLineRepoQueryReq.getBusinessType()));
        }
        if(CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getBusinessTypes())){
            List<Integer> queryBusinessTypes = orderLineRepoQueryReq.getBusinessTypes().stream().map(Integer::valueOf).collect(Collectors.toList());
            queryOrderLineRequest.setBusinessTypes(queryBusinessTypes);
        }
        //单据创建日期
        if (CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getGmtCreateList())) {
            queryOrderLineRequest.setGmtCreateStartTime(orderLineRepoQueryReq.getGmtCreateList().get(0) == null ? null : DateUtils.string2Date(orderLineRepoQueryReq.getGmtCreateList().get(0)).getTime());
            queryOrderLineRequest.setGmtCreateEndTime(orderLineRepoQueryReq.getGmtCreateList().get(1) == null ? null : DateUtils.string2Date(orderLineRepoQueryReq.getGmtCreateList().get(1)).getTime());
        }
        //单据修改日期
        if (CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getGmtUpdateList())) {
            queryOrderLineRequest.setGmtUpdateStartTime(orderLineRepoQueryReq.getGmtUpdateList().get(0) == null ? null : DateUtils.string2Date(orderLineRepoQueryReq.getGmtUpdateList().get(0)).getTime());
            queryOrderLineRequest.setGmtUpdateEndTime(orderLineRepoQueryReq.getGmtUpdateList().get(1) == null ? null : DateUtils.string2Date(orderLineRepoQueryReq.getGmtUpdateList().get(1)).getTime());
        }
        // 客户期待交货日期 区间查询
        if (CollectionUtils.isNotEmpty(orderLineRepoQueryReq.getCustomerExpectDateList())){
            if (orderLineRepoQueryReq.getCustomerExpectDateList().get(0) != null){
                queryOrderLineRequest.setCustomerExpectStartDate(DateUtils.string2Date(orderLineRepoQueryReq.getCustomerExpectDateList().get(0)).getTime());
            }
            if (orderLineRepoQueryReq.getCustomerExpectDateList().get(1) != null){
                queryOrderLineRequest.setCustomerExpectEndDate(DateUtils.string2Date(orderLineRepoQueryReq.getCustomerExpectDateList().get(1)).getTime());
            }
        }
        queryOrderLineRequest.setLimit(orderLineRepoQueryReq.getLimit());
        queryOrderLineRequest.setStart(orderLineRepoQueryReq.getStart());
        return queryOrderLineRequest;
    }
}
